package com.changjiang.luban.config.mvc;


import com.changjiang.luban.config.interceptor.TokenInterceptor;
import com.changjiang.luban.config.resolver.CurrentUserMethodArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * Description:自定义拦截器
 *
 * @author changjiang
 * Date: 2018-03-28
 * Time: 16:21
 */
@SuppressWarnings("ALL")
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {

    @Autowired
    private CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver;

    /**
     * 配置静态访问资源
     * @param registry 资源
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/myImage/**").addResourceLocations("file:C:/luban/file/img/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //拦截所有请求，通过判断是否有 @LoginRequired 注解 决定是否需要登录
        registry.addInterceptor(userInterceptor())
                .addPathPatterns("/admin/**").addPathPatterns("/");
        super.addInterceptors(registry);
    }

    @Bean
    public TokenInterceptor userInterceptor() {
        return new TokenInterceptor();
    }


    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(currentUserMethodArgumentResolver);
    }

   }
